package com.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.portal.model.entity.Book;
import com.portal.model.entity.BookType;
import com.portal.service.BookService;
import com.portal.service.BookTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 书籍分类
 */
@RestController
@RequestMapping("/book")
@Tag(name = "图书管理")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 热销榜: 查询所有的书籍
     */
    @PostMapping("/top")
    @Operation(summary = "查询图书")
    public ResponseEntity<Page<Book>> findBySales(@RequestBody Map<String,Integer> params) {
        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");
        Page<Book> pages = bookService.findTopSales(pageNum, pageSize);
        return ResponseEntity.ok(pages);
    }

    /**
     * 根据类别查询书籍
     */
    @PostMapping("/findByType")
    @Operation(summary = "根据类别查询书籍")
    public ResponseEntity<IPage<Book>> findByType(@RequestBody Map<String,Integer> params) {
        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");
        Integer typeId = params.get("typeId");
        IPage<Book> pages = bookService.findByType(pageNum, pageSize,typeId);
        return ResponseEntity.ok(pages);
    }

    /**
     * 通过ID查询书籍
     */
    @GetMapping("/{id}")
    @Operation(summary = "通过ID查询书籍")
    public ResponseEntity<Book> findById(@PathVariable("id") Long id) {
        Book book = bookService.findById(id);
        return ResponseEntity.ok(book);
    }
}